﻿<?php
if ($level != 3) {
	echo "Bạn không có quyền vào trang này.";
	exit();
}
$num = $mysql->query("SELECT * FROM ".$tb_prefix."tpl");
$total = $mysql->num_rows($num);
if($total) {
	$m_per_page = 30;
	if (!$pg) $pg = 1;
	$sql = "SELECT * FROM ".$tb_prefix."tpl ORDER BY tpl_id DESC LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
	$query = $mysql->query($sql);
		while($row = $mysql->fetch_array($query)) {
			$tpl_ID		=	$row['tpl_id'];
			$tpl_FNAME	=	$row['tpl_fname'];
			$tpl_ORDER	=	$row['tpl_order'];
			$tpl_HTML  .=	"<tr><td align=center class=fr><input onclick=this.select() type=text name='o".$tpl_ID."' value=".$tpl_ORDER." size=2 style='text-align:center'></td><td class=fr_2><a href='?act=tpl&del_id=".$tpl_ID."'>Xóa</a> - <a href=?act=list_tpl&mode=set_default&id=".$tpl_ID.">Mặc định</a> - <a href='?act=tpl&mode=edit&id=".$tpl_ID."'><b>".$tpl_FNAME."</b></a></td></tr>";
		}
}
if(!$total) {
	$user_HTML = "<tr><td></td><td align=center>Chưa có templates</td></tr>";
}
if ($_POST['sbm']) {
	$z = array_keys($_POST);
	$q = $mysql->query("SELECT tpl_id FROM ".$tb_prefix."tpl");
	for ($i=0;$i<$mysql->num_rows($q);$i++) {
		$id = split('o',$z[$i]);
		$ord = ${$z[$i]};
		$mysql->query("UPDATE ".$tb_prefix."tpl SET tpl_order = '$ord' WHERE tpl_id = '".$id[1]."'");
		echo "<script language='JavaScript'>{ window.parent.location='?act=list_tpl' }</script>";
	}
}
if ($mode == 'set_default' && is_numeric($id)) {
	$name = $mysql->fetch_array($mysql->query("SELECT tpl_sname FROM ".$tb_prefix."tpl WHERE tpl_id = '$id'"));
	if ($name) {
		$mysql->query("UPDATE ".$tb_prefix."config SET config_value = '".$name[0]."' WHERE config_name = 'default_tpl'");
		echo "<script language='JavaScript'>{ window.parent.location='?act=list_tpl' }</script>";
	}
	else echo "Lỗi.";
}
include("templates/list_tpl.html");
?>